package com.emser.unc.activitys.resources;

import java.util.ArrayList;
import java.util.List;

import com.emser.unc.R;
import com.emser.unc.serializable.ProductDTO;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ProductAdapter extends ArrayAdapter<ProductDTO> {
	
	private ArrayList<ProductDTO> entries;
	private Activity activity;

	public ProductAdapter(Activity a, int textViewResourceId,
			List<ProductDTO> values) {
		super(a, textViewResourceId, values);
		this.entries = (ArrayList<ProductDTO>) values;
		this.activity = a;
	}

	public static class ViewHolder {
		public TextView item0;
		public TextView item1;
		public TextView item2;
	}
	
	 @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	        View v = convertView;
	        ViewHolder holder;
	        if (v == null) {
	            LayoutInflater vi =
	                (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            v = vi.inflate(R.layout.r_product_row_view, null);
	            holder = new ViewHolder();
	            holder.item0 = (TextView) v.findViewById(R.id.product_code);
	            holder.item1 = (TextView) v.findViewById(R.id.product_title);
	            holder.item2 = (TextView) v.findViewById(R.id.product_price);
	            v.setTag(holder);
	        }
	        else{
	        	holder=(ViewHolder)v.getTag();
	        }
	        final ProductDTO custom = entries.get(position);
	        if (custom != null) {
	        	holder.item0.setText(custom.getCode().toString());
	            holder.item1.setText(custom.getTitle());
	            holder.item2.setText("$ "+custom.getUnitPrice().toString());
	        }
	        return v;
	    }
	 
	}